%include "ASMimplibs.sas";
 

%macro merge_dshk99(prod,data);

  /* Reformat the exit variable to be consistent with FHS do file for exit probits. */

  data all&prod._post&data (keep = _impute_ ppn id tvs fdeath lq lq2 lqphy ltfpphy ltfp ltfp2 lprice2 lk rnq2 year prod ); 
   set fhs7797.all&prod._post&data;
    prod = "&prod";
    rnq2=nq2/piship; 
    lk=log(kseq+ksst);
  run;


  proc sort data=fhs7797.dshk99_&prod out=dshk99_&prod; by _impute_ ppn year;run; 

  proc sort data=all&prod._post&data; by _impute_ ppn year; run;

  data fhs7797.all&prod.f_w_dshk_&data;
   merge  all&prod._post&data (in=inCART) dshk99_&prod (in=indshk99); 
   by _impute_ ppn year;
   if inCART and indshk99;
  run;

%mend;

%merge_dshk99(bred,CART);
%merge_dshk99(carbon,CART);
%merge_dshk99(coff,CART);
%merge_dshk99(flr,CART);
%merge_dshk99(gas,CART);
%merge_dshk99(iceb,CART);
%merge_dshk99(icep,CART);
%merge_dshk99(ply,CART);
%merge_dshk99(box,CART);
%merge_dshk99(conc,CART);


/* For sugar, we didn't need to create CART imputations, so we just use FHS's final sugar data
   and merge it with FHS's final demand shock data.  */

data allsugf (keep = _impute_ ppn id tvs fdeath lq lq2 lqphy ltfpphy ltfp ltfp2 lprice2 lk rnq2 year prod ); 
 set ppsr50.allsugf;
  prod = "sug";
  rnq2=nq2/piship; 
  lk=log(kseq+ksst);
  _impute_ = .;
run;


/* Get dshk99 for sugar from the FHS demandshk dataset and merge it with the final FHS sugar data. */ 

data demandshk_fhs; 
 set ppsr50.demandshk;
run;

proc sort data=demandshk_fhs; by ppn year; run;
proc sort data=allsugf; by ppn year; run;

data fhs7797.allsugf_dshk;
 merge allsugf (in=insug) demandshk_fhs (in=infhs);
 by ppn year;
 if insug and infhs;
run;

